<template>
  <div class="app-container">
    <!--医院设置表单-->
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname" />
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode" />
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl" />
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import hospsetApi from "@/api/syt/hospset";
export default {
  data() {
    return {
      hospset: {},
      saveBtnDisabled: false, // 保存按钮是否禁用,
    };
  },
  created() {
    console.log("created");
    if (this.$route.params.id) {
      this.fetchDataById(this.$route.params.id);
    }
  },
  methods: {
    // 根据id查询记录
    fetchDataById(id) {
      hospsetApi.getById(id).then((response) => {
        this.hospset = response.data;
      });
    },
    saveOrUpdate() {
      this.saveBtnDisabled = true;
      if (!this.hospset.id) {
        this.saveData();
      } else {
        this.updateData();
      }
    },
    // 保存
    saveData() {
      hospsetApi.save(this.hospset).then((response) => {
        this.$message.success(response.message);
        this.$router.push({ path: "/syt/hospset/list" });
      });
    },
    // 根据id更新记录
    updateData() {
      hospsetApi.updateById(this.hospset).then((response) => {
        this.$message.success(response.message);
        this.$router.push({ path: "/syt/hospset/list" });
      });
    },
  },
};
</script>